#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node* pre;
    struct node* next;
};
struct node* ListInit()
{
    struct node* phead=(struct node*)malloc(sizeof(struct node));
    phead->next=phead;
    phead->pre=phead;
    return phead;
};
void ListPushback(struct node* phead,int x)
{
    struct node* newnode=(struct node*)malloc(sizeof(struct node));
    newnode->data=x;
    struct node* tail;
    tail=phead->pre;
    tail->next=newnode;
    newnode->pre=tail;
    newnode->next=phead;
    phead->pre=newnode;
}
void ListPop(struct node* cur)
{
    struct node* ne=cur->next;
    struct node* prev=cur->pre;
    prev->next=ne;
    ne->pre=prev;
    free(cur);
    cur=NULL;
}
int main()
{
    int n,m,x;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        struct node* plist;
        plist=NULL;
        plist=ListInit();
        for(int i=0;i<n;i++)
        {
            ListPushback(plist,i);
        }
        struct node* cur=plist->next;
        int k=1;
        while(plist->next->next!=plist)
        {
            if(k!=m)
            {
                if(cur->next==plist)
                {
                    cur=cur->next->next;
                }
                else
                {
                    cur=cur->next;
                }
                k++;
            }
            else
            {
                ListPop(cur);
                k=0;
            }
        }
        printf("%d",plist->next->data);
    }
    return 0;
}
